package com.traffic.between.config;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.traffic.between.annotation.InterfaceProviderPlugin;
import com.traffic.between.plugin.RedisOrderQueuePlugin;
import com.traffic.between.redis.Cache;
import com.traffic.between.redis.IKeyNamingPolicy;
import com.traffic.between.redis.Redis;
import com.traffic.between.redis.RedisPlugin;
import com.traffic.between.redis.serializer.JRESerializer;
import com.traffic.between.util.DictConstantKeys;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * JFinal加载配置文件
 */
public class BetweenConfig extends JFinalConfig {
	private static Logger logger = Logger.getLogger(BetweenConfig.class);

	// 常量配置
	@Override
	public void configConstant(Constants me) {
		File file = new File("/data/wwwroot/conf/between.properties");//线上环境
		if (file.exists()) {
			loadPropertyFile(file);

		}else {
			loadPropertyFile("between.properties");
		}
		

		// 设置字符集
		me.setEncoding("UTF-8");
		// 设置开发模式
		me.setDevMode(getPropertyToBoolean("devMode", false));
	}

	// 处理器配置 接管所有web请求，可在此层做功能性的拓展
	@Override
	public void configHandler(Handlers me) {
	}

	// 拦截器配置 类似与struts2拦截器，处理action
	@Override
	public void configInterceptor(Interceptors me) {

		// me.add(new TxByMethods("find","update","delete","save")); //声明式事物管理

	}

	/** 插件配置 JFinal集成了很多插件：redis,druid,quartz... **/
	@Override
	public void configPlugin(Plugins me) {

		// 加载redisOrderPlugin
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		JedisPool jedisPool = new JedisPool(jedisPoolConfig, getProperty("redis.host"),
				Integer.valueOf(getProperty("redis.port")), 1800,getProperty("redis.password"),15);
		
		Cache cache = new Cache(DictConstantKeys.redis_order_name, jedisPool, JRESerializer.me,
				IKeyNamingPolicy.defaultKeyNamingPolicy);
		Redis.addCache(cache);
		
		me.add(new InterfaceProviderPlugin());
		// redisOrderPlugin 加载redis队列插件
		RedisOrderQueuePlugin queuePlugin = new RedisOrderQueuePlugin();
		me.add(queuePlugin);
		//加载redis系统插件
		RedisPlugin redisSysPlugin = new RedisPlugin(DictConstantKeys.redis_sys_name, getProperty("redis.host"),
				 Integer.valueOf(getProperty("redis.port")),1800,getProperty("redis.password"),14);
		redisSysPlugin.getJedisPoolConfig().setMaxIdle(40);
		redisSysPlugin.getJedisPoolConfig().setMaxTotal(40);
		me.add(redisSysPlugin);
	

	}

	@Override
	public void configRoute(Routes me) {

	}

	// jfinal关闭之前
	public void beforeJFinalStop() {
		logger.info("BetweenConfig于 " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + "  插件关闭");
	};

	// jfinal插件加载完后
	public void afterJFinalStart() {
		logger.info("BetweenConfig于 " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + "  加载完毕");
	};

}
